"use strict"
var fs = require('fs')
var path = require('path')
var rf = require('fs')

module.exports = function(dir) {
  var templates = []
  if (!fs.existsSync(dir)) {
    return null
  }
  let files = fs.readdirSync(dir)
  files.forEach(function(filename) {
    let templatePath = path.join(dir, `/${filename}`)
    let data = rf.readFileSync(templatePath,"utf-8")
    let datas = JSON.parse(data)
    let modules = []
    for(var i = 0; i < datas.topics.length; i ++) {
      modules.push({
        id: datas.topics[i].id,
        topic: datas.topics[i]
      })
    }
    if (fs.statSync(dir).isDirectory()) {
      let name = datas.type
      templates.push({
        type: name + '/' + datas.version,
        schema: datas.settingsSchema,
        modules: modules
      })
    }
  })
  return templates
}